package com.muyu.danmo.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoTrainingLikes;
import com.muyu.danmo.service.IDanmoTrainingLikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 培训班点赞Controller
 *
 * @author yangle
 * @date 2024-10-12
 */
@RestController
@RequestMapping("/likes")
public class DanmoTrainingLikesController extends BaseController
{
    @Autowired
    private IDanmoTrainingLikesService danmoTrainingLikesService;

    /**
     * 查询培训班点赞列表
     */
//    @RequiresPermissions("system:likes:list")
    @GetMapping("/list")
    public Result<TableDataInfo<DanmoTrainingLikes>> list(DanmoTrainingLikes danmoTrainingLikes)
    {
        startPage();
        List<DanmoTrainingLikes> list = danmoTrainingLikesService.selectDanmoTrainingLikesList(danmoTrainingLikes);
        return getDataTable(list);
    }

    /**
     * 导出培训班点赞列表
     */
//    @RequiresPermissions("system:likes:export")
    @Log(title = "培训班点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoTrainingLikes danmoTrainingLikes)
    {
        List<DanmoTrainingLikes> list = danmoTrainingLikesService.selectDanmoTrainingLikesList(danmoTrainingLikes);
        ExcelUtil<DanmoTrainingLikes> util = new ExcelUtil<DanmoTrainingLikes>(DanmoTrainingLikes.class);
        util.exportExcel(response, list, "培训班点赞数据");
    }

    /**
     * 获取培训班点赞详细信息
     */
//    @RequiresPermissions("system:likes:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return success(danmoTrainingLikesService.selectDanmoTrainingLikesById(id));
    }

    /**
     * 新增培训班点赞
     */
//    @RequiresPermissions("system:likes:add")
    @Log(title = "培训班点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoTrainingLikes danmoTrainingLikes)
    {
        return toAjax(danmoTrainingLikesService.insertDanmoTrainingLikes(danmoTrainingLikes));
    }

    /**
     * 修改培训班点赞
     */
//    @RequiresPermissions("system:likes:edit")
    @Log(title = "培训班点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoTrainingLikes danmoTrainingLikes)
    {
        return toAjax(danmoTrainingLikesService.updateDanmoTrainingLikes(danmoTrainingLikes));
    }

    /**
     * 删除培训班点赞
     */
//    @RequiresPermissions("system:likes:remove")
    @Log(title = "培训班点赞", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return toAjax(danmoTrainingLikesService.deleteDanmoTrainingLikesByIds(ids));
    }

    @GetMapping("/like/{trainingInformationId}")
    public Result likeTrainingClass(@PathVariable Long trainingInformationId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null || loginUser.getUserid() == null) {
            return Result.notLoggedIn("请登录");
        }
        Long userId = loginUser.getUserid();
        if (userId.equals(trainingInformationId)) {
            return Result.error("不能点赞自己");
        }

        return danmoTrainingLikesService.likeTrainingClass(userId, trainingInformationId);
    }

    /**
     *
     * @description: 获取点赞数量
     * @param trainingInformationId
     * @return
     */
    @GetMapping("/likes/{trainingInformationId}")
    public Long getLikesCount(@PathVariable Long trainingInformationId) {
        return danmoTrainingLikesService.getLikesCount(trainingInformationId);
    }

}
